package com.example.peibei.ui.activity;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dingtao.common.core.WDActivity;
import com.dingtao.common.util.FileCacheUtils;
import com.dingtao.common.util.SPUtils;
import com.dingtao.common.util.UIUtils;
import com.example.peibei.R;
import com.example.peibei.base.SpConstant;
import com.example.peibei.ui.weight.CenterDialog;
import com.linxiao.framework.dialog.BottomDialog;
import com.tencent.imsdk.v2.V2TIMCallback;
import com.tencent.qcloud.tuicore.TUILogin;

import java.io.File;

import butterknife.BindView;
import butterknife.OnClick;

public class SettingActivity extends WDActivity {
    @BindView(R.id.tv_cache)
    TextView tv_cache;
    @BindView(R.id.tv_version)
    TextView tv_version;

    private SPUtils spUtils;
    private String mid;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_setting;
    }

    @Override
    protected void initView() {
        initCacheSize();
        spUtils = new SPUtils(SettingActivity.this, SpConstant.USER_TOKEN);
        mid = spUtils.getLong(SpConstant.USER_MID) + "";
        tv_version.setText(getAppVersionName(this));
    }

    /**
     * 返回当前程序版本名称
     */

    public static String getAppVersionName(Context context) {
        String versionName = "";
        try {
            PackageManager pm = context.getPackageManager();
            PackageInfo pi = pm.getPackageInfo("com.example.peibei", 0);
            versionName = pi.versionName;
            if (TextUtils.isEmpty(versionName)) {
                return "";
            }

        } catch (Exception e) {
        }
        return versionName;
    }

    @OnClick(R.id.iv_back)
    public void back() {
        finish();
    }

    @OnClick(R.id.rl_account_safe)
    public void setAccountSafe() {
        intent(AccountSafeActivity.class);
    }

    @OnClick(R.id.rl_bind_account)
    public void rl_bind_account() {
        intent(BindPayAccountActivity.class);
    }

    @OnClick(R.id.rl_privacy_setting)
    public void setPrivacy() {
        intent(PrivacySettingActivity.class);
    }

    @OnClick(R.id.rl_clean_cache)
    public void cleanCache() {
        showDialog();
    }

    @OnClick(R.id.rl_message_notice)
    public void messageNotice() {
        intent(NoticeSettingActivity.class);
    }

    @OnClick(R.id.rl_about_us)
    public void aboutUs() {
        intent(AboutUsActivity.class);
    }

    @OnClick(R.id.btn_login_out)
    public void loginOut() {
        showLoginOutDialog();
    }

    @OnClick(R.id.rl_person_info)
    public void personInfo() {
//        Bundle bundle = new Bundle();
//        bundle.putString("mid", mid);
        intent(EditInfoActivity.class);
    }

    private void showDialog() {
        View dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_bottom_view, null);
        BottomDialog bottomDialog = new BottomDialog(this);
        bottomDialog.setContentView(dialogView);
        bottomDialog.setCanceledOnTouchOutside(true);
        bottomDialog.show();

        TextView tv_cancel = dialogView.findViewById(R.id.tv_cancel);
        TextView tv_sure = dialogView.findViewById(R.id.tv_sure);
        TextView tv_title = dialogView.findViewById(R.id.tv_title);
        tv_title.setText("确定清除缓存吗？");
        tv_cancel.setOnClickListener(v -> bottomDialog.dismiss());
        tv_sure.setOnClickListener(v -> {
            FileCacheUtils.cleanExternalCache(this);
            initCacheSize();
            UIUtils.showToastSafe("已清除缓存");
            bottomDialog.dismiss();
        });
    }

    private void showLoginOutDialog() {
        View dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_center_view, null);
        CenterDialog bottomDialog = new CenterDialog(this);
        bottomDialog.setContentView(dialogView);
        bottomDialog.setCanceledOnTouchOutside(true);
        bottomDialog.show();

        TextView tv_cancel = dialogView.findViewById(R.id.tv_cancel);
        TextView tv_sure = dialogView.findViewById(R.id.tv_sure);
        TextView tv_title = dialogView.findViewById(R.id.tv_title);
        tv_title.setText("确定退出登录吗？");
        tv_cancel.setOnClickListener(v -> bottomDialog.dismiss());
        tv_sure.setOnClickListener(v -> {
            spUtils.clear();
            spUtils.putBoolean(SpConstant.USER_AGREE, true);
//            TUILogin.logout(new V2TIMCallback() {
//                @Override
//                public void onSuccess() {
//                    Log.i("answer", "退出聊天");
//                    TUILogin.unInit();
//                }
//
//                @Override
//                public void onError(int i, String s) {
//
//                }
//            });
            SpConstant.outLogin();
            intent(LoginActivity.class);
            bottomDialog.dismiss();
            finish();
        });
    }


    private void initCacheSize() {
    /*
    * 获取SD卡根目录：Environment.getExternalStorageDirectory().getAbsolutePath();
        外部Cache路径：/mnt/sdcard/android/data/com.xxx.xxx/cache 一般存储缓存数据（注：通过getExternalCacheDir()获取）
        外部File路径：/mnt/sdcard/android/data/com.xxx.xxx/files 存储长时间存在的数据
        （注：通过getExternalFilesDir(String type)获取， type为特定类型，可以是以下任何一种
                    Environment.DIRECTORY_MUSIC,
                    Environment.DIRECTORY_PODCASTS,
                     Environment.DIRECTORY_RINGTONES,
                     Environment.DIRECTORY_ALARMS,
                     Environment.DIRECTORY_NOTIFICATIONS,
                     Environment.DIRECTORY_PICTURES,
                      Environment.DIRECTORY_MOVIES. ）
    * */
        String sdPath = Environment.getExternalStorageDirectory().getAbsolutePath();
        File outCachePath = getExternalCacheDir();
        File outFilePath = getExternalFilesDir(Environment.DIRECTORY_ALARMS);

        try {
            String outCacheSize = FileCacheUtils.getCacheSize(outCachePath);
            String outFileSize = FileCacheUtils.getCacheSize(outFilePath);
            tv_cache.setText(outCacheSize);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void destoryData() {

    }
}
